package com.situ.onlinebookstore.front.service.impl;

import com.situ.onlinebookstore.common.entity.User;
import com.situ.onlinebookstore.common.mapper.UserMapper;
import com.situ.onlinebookstore.common.util.MD5Util;
import com.situ.onlinebookstore.front.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;

    @Override
    public User login(User user) throws Exception {
        User sUser = userMapper.selectByUsername(user.getUsername());
        if (ObjectUtils.isEmpty(sUser)) {
            throw new Exception("用户名不存在！");
        }
        String md5Pwd = MD5Util.getDBMD5(user.getPassword(), sUser.getSalt());
        if (!sUser.getPassword().equals(md5Pwd)) {
            throw new Exception("密码不正确！");
        }
        //验证状态 枚举
        if (sUser.getStatus().equals(User.STATUS_DISABLE)) {
            throw new Exception("用户已被禁用！");
        }
        return sUser;
    }


    @Override
    public int changePassword(String oldPassword, String newPassword, Integer id) throws Exception {
        //        新密码与旧密码不能一致
        if (oldPassword.equals(newPassword)) {
            throw new Exception("新旧密码不能一样！");
        }

        //判断旧密码是否正确
        User user = userMapper.selectById(id);
        String md5Pwd = MD5Util.getDBMD5(oldPassword, user.getSalt());
        if (!md5Pwd.equals(user.getPassword())) {
            throw new Exception("旧密码错误");
        }


        //新密码加密
        newPassword = MD5Util.getDBMD5(newPassword, user.getSalt());
        user.setPassword(newPassword);
        //修改数据库
        return userMapper.update(user);
    }

    @Override
    public int rePassword(String username, String password, String rePassword) throws Exception {
        User user = userMapper.selectByUsername(username);

        if (!Objects.equals(username, user.getUsername())) {
            throw new Exception("用户名不存在");
        }
        //        新密码与旧密码不能一致
        if (!password.equals(rePassword)) {
            throw new Exception("两次密码不一样！");
        }

        //判断旧密码是否正确
        String md5Pwd = MD5Util.getDBMD5(password, user.getSalt());
        //新密码加密

        user.setPassword(md5Pwd);
        //修改数据库
        return userMapper.update(user);
    }

    @Override
    public int confirmPwd(String username, String password) throws Exception {
        User user = userMapper.selectByUsername(username);
        if (user != null) {
            throw new Exception("用户名存在");
        }
        if (username == null) {
            throw new Exception("用户名不能为空");
        }
        User sUser = new User();
        String salt = UUID.randomUUID().toString().substring(0, 8);
        String md5Pwd = MD5Util.getDBMD5(password, salt);
        //新密码加密
        sUser.setUsername(username);
        sUser.setSalt(salt);
        sUser.setPassword(md5Pwd);
        sUser.setCreateTime(LocalDateTime.now());

        //修改数据库
        return userMapper.insert(sUser);
    }


}
